package com.douwong.student.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.douwong.student.R;
import com.douwong.student.model.ClassTestRecordInfos;
import com.douwong.student.popuwindow.ClassTestRecordPopuWindow;
import com.douwong.student.utils.HtmlUtils;
import com.douwong.student.utils.UIUtils;

import java.util.List;

/**
 * @autor yichao
 * @time 2017/5/1016:55
 * @des 课堂测试记录 选择题
 */
@SuppressLint("ValidFragment")
public class BaseClassTestRecordChoiceFragment extends BaseFragment {
    private View mContenView;
    private AppCompatTextView mAppCompatTextView;
    private ImageView mIvHead;
    private LinearLayout mLlTitleRoot;
    protected int[] linearlayouts = new int[] { R.id.ll_A_choice, R.id.ll_B_choice, R.id.ll_C_choice, R.id.ll_D_choice,
            R.id.ll_E_choice, R.id.ll_F_choice, R.id.ll_G_choice, R.id.ll_H_choice };
    protected int[] textViews = new int[] { R.id.tv_choose_A, R.id.tv_choose_B, R.id.tv_choose_c, R.id.tv_choose_D,
            R.id.tv_choose_e, R.id.tv_choose_F, R.id.tv_choose_g, R.id.tv_choose_H};

    private String[] chart = { "A", "B", "C", "D", "E", "F", "G", "H" };
    private static final String TAG = "yichaos";
    private Context mContext;
    private ClassTestRecordPopuWindow mShowClassTestRecordPopuWindown;
    private ClassTestRecordInfos.DataBean.ExaminResultBean mExaminResultBean;
    private final ClassTestRecordInfos.DataBean.ExaminResultBean.IteminfoBean mIteminfo;
    private final List<String> mAnswer;


    @Override
    public void onAttach(Context context) {

        super.onAttach(context);
        mContext = context;
    }

    public BaseClassTestRecordChoiceFragment(ClassTestRecordInfos.DataBean.ExaminResultBean examinResultBean) {
        super();
        mExaminResultBean = examinResultBean;
        mIteminfo = examinResultBean.getIteminfo();
        mAnswer = mExaminResultBean.getAnswer();
    }

    @Override
    protected View getRootView(LayoutInflater inflater, ViewGroup container) {
        mContenView = UIUtils.inflate(mContext, R.layout.base_test_record_choice_questions);
        mIvHead = (ImageView) mContenView.findViewById(R.id.iv_answer_show_pic);
        mAppCompatTextView = (AppCompatTextView) mContenView.findViewById(R.id.tv_question_choice);
        mLlTitleRoot = (LinearLayout) mContenView.findViewById(R.id.ll_choicefragme_title_root);
        return mContenView;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mIvHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mShowClassTestRecordPopuWindown = new ClassTestRecordPopuWindow(mContext,mIteminfo);
                mShowClassTestRecordPopuWindown.showAtLocation(v, Gravity.CENTER,0,0);
            }
        });
        mLlTitleRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mShowClassTestRecordPopuWindown = new ClassTestRecordPopuWindow(mContext,mIteminfo);
                mShowClassTestRecordPopuWindown.showAtLocation(v, Gravity.CENTER,0,0);

            }
        });

    }

    @Override
    protected void initData() {
        super.initData();
        if (mIteminfo!=null){
            String itemBody = mIteminfo.getItem_body();
            //是否显示图片
            mIvHead.setVisibility(HtmlUtils.isShowImage(itemBody)?View.VISIBLE:View.GONE);
            //题目
            HtmlUtils.showItemBody(mAppCompatTextView,itemBody);
            //选项

            List<ClassTestRecordInfos.DataBean.ExaminResultBean.IteminfoBean.ItemOptionsBean> item_options = mIteminfo.getItem_options();
       if (item_options!=null && item_options.size()>0){
           for (int i = 0; i < item_options.size(); i++) {
               ClassTestRecordInfos.DataBean.ExaminResultBean.IteminfoBean.ItemOptionsBean itemOptionsBean = item_options.get(i);
               mContenView.findViewById(linearlayouts[i]).setVisibility(View.VISIBLE);
               //设置值
               TextView text = (TextView) mContenView.findViewById(textViews[i]);
           //    ImagFilterUtil.filterImage(text,itemOptionsBean.getValue(),1);
               HtmlUtils.showItemBody(text,itemOptionsBean.getValue());
           }
       }



            //设置选中的item
            showResult();

        }
    }
    public void showResult() {
        //查看考试时显示

            for (int j = 0; j < linearlayouts.length; j++) {
                mContenView.findViewById(linearlayouts[j]).setClickable(false);

            if (mAnswer == null ||mAnswer.size()==0) {

                return;
            }


                for (int i = 0; i < mAnswer.size(); i++) {

                    if (mIteminfo.getItem_type() == 1) {// 单选

                        setSingleViewState(mAnswer.get(i));
                    } else if (mIteminfo.getItem_type() == 2) {// 多选

                        setMulteViewState(mAnswer.get(i));
                    }
                }






        }
    }

    // 单选设置控件状态
    private void setSingleViewState(String charts) {
        for (int i = 0; i < linearlayouts.length; i++) {
            if (charts.equalsIgnoreCase(chart[i])) {
                mContenView.findViewById(linearlayouts[i]).setSelected(true);
            } else {
                mContenView.findViewById(linearlayouts[i]).setSelected(false);
            }
        }
    }

    // 设置多选答案显示状态
    private void setMulteViewState(String charts) {
        for (int i = 0; i < linearlayouts.length; i++) {
            if (charts.contains(chart[i])) {
                mContenView.findViewById(linearlayouts[i]).setVisibility(View.VISIBLE);
                mContenView.findViewById(linearlayouts[i]).setSelected(true);
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mShowClassTestRecordPopuWindown!=null && mShowClassTestRecordPopuWindown.isShowing()){
            mShowClassTestRecordPopuWindown.dismiss();

        }
    }
}
